package cn.agiledata.bank.common.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.agiledata.bank.hibernateBean.ActionType;
import cn.agiledata.bank.hibernateBean.BankInfo;
import cn.agiledata.bank.hibernateBean.ModelType;
import cn.agiledata.bank.hibernateBean.Sysbook;
import com.vnv.infrastructure.logic.IPBHibernate3BaseMgr;

public class ActionTypeTemplate extends IPBHibernate3BaseMgr {
	
	public List getActionTypes(){
		List resultList =getHibernateTemplate().find("from ActionType order by id ");
		//getHibernateTemplate().flush();
		return resultList;
	}
	
	public List getModelTypeByIds(final Long[] ids){
		final String sql = "from ModelType where id in (:ids) order by id ";
		
		return getObjectsByIds(ids, sql);			
	}
	
	public List getActionTypeByIds(final Long[] ids){
		final String sql = "from ActionType where id in (:ids) order by id ";
		
		return getObjectsByIds(ids, sql);		
	}

	private List getObjectsByIds(final Long[] ids, final String sql) {
		List resultList = (List)getHibernateTemplate().execute(
				new HibernateCallback(){
					public Object doInHibernate(Session session) throws HibernateException {
						
						Query query = session.createQuery(sql)
						.setParameterList("ids", ids);
						return query.list();
					}
				},true);
		
		//getHibernateTemplate().flush();
		return resultList;
	}
	public ActionType getActionTypeById(long id){
		List resultList =getHibernateTemplate().find("from ActionType where id = ? ",new Object[]{new Long(id)});
		//getHibernateTemplate().flush();
		return (ActionType)resultList.get(0);
	}
        
        public ActionType getActionTypeByActionId(String actionId){
            String hql = "from ActionType at where at.actionId=?";
            List resultList = getHibernateTemplate().find(hql, actionId);
            return resultList.size() == 0 ? null: (ActionType) resultList.get(0);
        }
	
	public ModelType getModelType(String modelType) {
		ModelType mt = 
			(ModelType)getHibernateTemplate().find("from ModelType where modelId =?",new Object[]{modelType}).get(0);
		return mt;
	}
	
	public List getAllModelTypes(){
		return getHibernateTemplate().find("from ModelType ");
	}
	
	public BankInfo getBankInfoById(String id) {
		String hql = "from BankInfo b where b.id=" + id;
		return (BankInfo) getHibernateTemplate().find(hql).get(0);
		
	}
	public Sysbook getTransStatusById(String id){
		String hql = "from Sysbook s where s.id.typeId = 'TRANS_STATUS' and s.id.listId = "+id;
		return (Sysbook)getHibernateTemplate().find(hql).get(0);
		
	}
	
}